package com.bistu.xiong.factory;

import android.support.annotation.StringRes;

import com.bistu.xiong.common.app.Application;
import com.bistu.xiong.factory.data.DataSource;
import com.bistu.xiong.factory.model.api.RspModel;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by XiongXiaoJun on 2017/12/17.
 */

public class Factory {
    // 单例模式
    private static final Factory instance;
    // 全局的线程池
    private final Executor executor;

    static {
        instance = new Factory();
    }
    private Factory(){
        // 新建一个4个线程的线程池
        executor = Executors.newFixedThreadPool(4);
    }

    /**
     * 返回全局的Application
     * @return Application
     */
    public static Application app() {
        return Application.getInstance();
    }

    /**
     * 异步运行的方法
     * @param runnable Runnable
     */
    public static void runOnAsync(Runnable runnable) {
        // 拿到单例，拿到线程池，然后异步执行
        instance.executor.execute(runnable);
    }

    /**
     * 进行错误Code的解析
     * 把网络返回的Code值进行统一的规划并返回为一个String资源
     *
     * @param model RspModel
     * @param callback DataSource.FailedCallback 用于返回一个错误的资源Id
     */
    public static void decodeRspCode(RspModel model, DataSource.FailedCallback callback) {
        if (model == null){
            decodeRspCode(R.string.data_rsp_error_service, callback);
            return;
        }


        // 进行Code区分
        switch (model.getCode()) {
            case RspModel.SUCCEED:
                return;
            case RspModel.ERROR_EXIST_NAME:
                decodeRspCode(R.string.data_rsp_error_parameters_exist_name, callback);
                break;
            case RspModel.ERROR_EXIST_PHONE:
                decodeRspCode(R.string.data_rsp_error_parameters_exist_phone, callback);
                break;
            case RspModel.ERROR_NEED_LOGIN:
                decodeRspCode(R.string.data_rsp_error_need_login, callback);
                break;
            case RspModel.ERROR_NOT_EXIST_USER:
                decodeRspCode(R.string.data_rsp_error_not_found_user, callback);
                break;
            case RspModel.ERROR_NOT_EXIST_TOKEN:
                decodeRspCode(R.string.data_rsp_error_not_found_token, callback);
                break;
            case RspModel.ERROR_NOT_FOUND_USER:
                decodeRspCode(R.string.data_rsp_error_not_found_user, callback);
                break;
            case RspModel.ERROR_USER_PASSWORD:
                decodeRspCode(R.string.data_account_login_error_password, callback);
                break;
            case RspModel.ERROR_PARAMETERS:
                decodeRspCode(R.string.data_rsp_error_parameters, callback);
                break;
            case RspModel.ERROR_UNKNOWN:
            default:
                decodeRspCode(R.string.data_rsp_error_unknown, callback);
                break;
        }
    }
    private static void decodeRspCode(@StringRes final int resId,final DataSource.FailedCallback callback) {
        if (callback != null)
            callback.onDataNotAvailable(resId);
    }

}
